package cn.dai;

import org.junit.Test;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author ArkD42
 * @file 1 - Java基础语法
 * @create 2020 - 06 - 02 - 10:40
 */
public class Base64Test {

    @Test // BASE64编码   字符串 -> 编码字符
    public void base64Encoding() throws UnsupportedEncodingException {
        String text = "这是一段演示文本";

        byte[] textBytes = text.getBytes("utf-8");

        BASE64Encoder base64Encoder = new BASE64Encoder();

        String encodeText = base64Encoder.encode(textBytes);

        System.out.println(encodeText);
    }

    @Test // BASE64解码   字符串 -> 解码字符
    public void base64Decoding() throws IOException {
        String base64CodeText = "6L+Z5piv5LiA5q615ryU56S65paH5pys";

        BASE64Decoder base64Decoder = new BASE64Decoder();

        byte[] decodeBuffer = base64Decoder.decodeBuffer(base64CodeText);

        String originText = new String(decodeBuffer);

        System.out.println(originText);
    }
}

